set more off

use "dataset_illinois", clear

local X policyself income age ideology ideology2 party

local j = 1

forvalues k = 15(3)27 {

	rename q`k'_1_text race1`j'
	rename q`k'_2_text policy1`j'
	rename q`k'_3_text neighbor1`j'
	rename q`k'_4_text experience1`j'

	rename q`k'_5_text race2`j'
	rename q`k'_6_text policy2`j'
	rename q`k'_7_text neighbor2`j'
	rename q`k'_8_text experience2`j'

	rename q`k'_9_text order`j'
	
	local j = `j' + 1

}

local j = 1

forvalues i = 14(3)26 {

	rename q`i' choice`j'
	destring choice`j', replace
	
	gen choice1`j' = 1 if choice`j' == 1
	replace choice1`j' = 0 if choice`j' == 2
	
	gen choice2`j' = 1 if choice`j' == 2
	replace choice2`j' = 0 if choice`j' == 1
	
	drop choice`j'
	
	local j = `j' + 1
	
}

keep V1 race* policy* neighbor* experience* order* choice* `X'

reshape i V1
reshape j pair
reshape xij race1 race2 policy1 policy2 neighbor1 neighbor2 experience1 experience2 order choice1 choice2
reshape xi `X'
reshape long

keep if pair < 3

replace policy1 = "1 " + "Supports cuts" if regexm(policy1, "Supports cut")
replace policy1 = "2 " + "Supports maintaining" if regexm(policy1, "Supports maintain")
replace policy1 = "3 " + "Supports increase" if regexm(policy1, "Supports increas")
encode policy1, gen(policyc1)
replace policyc1 = (policyc1 - 1)/(2)

replace policy2 = "1 " + "Supports cuts" if regexm(policy2, "Supports cut")
replace policy2 = "2 " + "Supports maintaining" if regexm(policy2, "Supports maintain")
replace policy2 = "3 " + "Supports increase" if regexm(policy2, "Supports increas")
encode policy2, gen(policyc2)
replace policyc2 = (policyc2 - 1)/(2)

gen distance1 = abs(policyself - policyc1)
gen distance2 = abs(policyself - policyc2)

egen id = group(V1 pair)
reshape i id
reshape j candidate
reshape xij race policy neighbor experience choice distance
reshape xi V1 order pair `X'
reshape long

replace policy = "1 " + "Supports cuts" if regexm(policy, "Supports cut")
replace policy = "2 " + "Supports maintaining" if regexm(policy, "Supports maintain")
replace policy = "3 " + "Supports increase" if regexm(policy, "Supports increas")

replace experience = " 2 years" if experience == "2 years"
replace experience = " 5 years" if experience == "5 years"
replace experience = experience + " experience"

encode policy, gen(policyc)
replace policyc = (policyc - 1)/(2)
gen proximity = -abs((policyself - policyc))
drop policyc

tostring proximity, replace 
drop if proximity == "."
replace proximity = "-1/2"  if proximity == "-.5"
replace proximity = proximity + " issue distance"

gen index = _n
local k = 1
foreach x of varlist race proximity neighbor experience {

	xi: reg choice i.`x', cl(V1)
	mat B`k' = e(b)
	svmat B`k'
	mat varr = vecdiag(e(V))
	matmap varr SE`k' , m(sqrt(@))
	svmat SE`k'
	
	local m = 1
	egen nvalues`k' = nvals(`x')
	levelsof `x', local(levels)
	encode `x', gen(`x'c)
	foreach l of local levels {
		if `m' != nvalues`k' {
			gen index2 = 1 if `x'c == `m' + 1
			sort index2
			gen label`k'`m' = `x'[1]
			local m = `m' + 1
			drop index2
		}
		if `m' == nvalues`k' {
			gen index2 = 1 if `x'c == 1
			sort index2
			gen label`k'`m' = `x'[1]
			local m = `m' + 1
			drop index2
		}
	}
	
	local k = `k' + 1
	
}

l B11 label11 if !mi(B11)

keep B* SE* label*
gen index = _n
reshape i index
reshape j value 
reshape xij B1 B2 B3 B4 SE1 SE2 SE3 SE4 label1 label2 label3 label4
reshape xi 
reshape long
drop index
gen index = _n

reshape i index
reshape j variable
reshape xij B SE label
reshape long

keep if !mi(B)

egen maxval = max(value), by(variable)
replace B = 0 if value == maxval

replace value = 0 if value == maxval

drop index

sort variable value

gen index = _n

gen UB = B + 1.96*SE if value != 0
gen LB = B - 1.96*SE if value != 0

labmask index, values(label)

#delimit;

gr tw 
	(sc index B if variable != 2, col(black)) 
	(rspike UB LB index if variable, horiz col(black))
	(sc index B if variable == 2, col(black) msym(O) mfcol(white)) 
	,
		yscale(rev)
		ylab(1(1)13, valuelabel angle(horiz))
		xline(0)
		xtitle("Marginal effect")
		legend(off)
		name(g`j', replace)
		ytitle("")
		plotregion(style(none))
		;
		
#delimit cr

gr export "figure 5.eps", replace
shell epstopdf "figure 5.eps"

list
